﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

namespace Cherry.Agile.ServiceModel.Routing {
    internal sealed class ServiceMessageReadWriteRoundRobin : IServiceMessageReadWriteRoundRobin {
        ReadWriteServiceEndpointTable _table = new ReadWriteServiceEndpointTable();
        public ServiceMessageReadWriteRoundRobin() {
            _table.Load();
        }
        public TFilterData GetMatchingValue<TFilterData>(Message msg) {
            var actionName = msg.Headers.Action;
            object val = new ServiceEndpoint[]{_table.GetServiceEndpoint(actionName)};
            return (TFilterData) val;
        }

        public bool GetMatchingValues<TFilterData>(Message message, ICollection<TFilterData> results) {
            var item = GetMatchingValue<TFilterData>(message);
            if (item == null)
                return false;
            results.Add(item);
            return true;
        }
    }
}
